package com.potenza.training.dictionary.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.dictionary.dao.SubjectTypeDao;
import com.potenza.training.dictionary.entity.SubjectTypeEntity;
import com.potenza.training.dictionary.server.SubjectTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("subjectTypeService")
public class SubjectTypeServiceImpl extends ServiceImpl<SubjectTypeDao, SubjectTypeEntity> implements SubjectTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    SubjectTypeDao subjectTypeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubjectTypeEntity> page = this.page(
                new Query<SubjectTypeEntity>().getPage(params),
                new QueryWrapper<SubjectTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SubjectTypeEntity> findAll() {
        Object subjectTypeEntity = redisTemplate.opsForValue().get("subjectTypeEntity");
        if (subjectTypeEntity==null){
            List<SubjectTypeEntity> list = this.list();
            redisTemplate.opsForValue().set("subjectTypeEntity", list);
            return list;
        }
        return (List<SubjectTypeEntity>) subjectTypeEntity;
    }



    @Override
    public SubjectTypeEntity findAllByName(String subjectType) {
        QueryWrapper<SubjectTypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SubjectTypeEntity::getTypeName,subjectType);
        return subjectTypeDao.selectOne(queryWrapper);
    }

    @Override
    public List<SubjectTypeEntity> findTypeByPid(int pid) {
        QueryWrapper<SubjectTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SubjectTypeEntity::getPid,pid);
        return subjectTypeDao.selectList(wrapper);
    }
}